package com.spyx.activity.api;

import com.atyx.spyx.model.activity.CouponInfo;
import com.atyx.spyx.model.order.CartInfo;
import com.atyx.spyx.vo.order.CartInfoVo;
import com.atyx.spyx.vo.order.OrderConfirmVo;
import com.spyx.activity.service.ActivityInfoService;
import com.spyx.activity.service.CouponInfoService;
import com.spyx.activity.service.CouponUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/activity")
public class ActivityApiController {

    @Autowired
    private ActivityInfoService activityInfoService;

    //根据skuidList,查询出每件商品的活动规则
    @PostMapping("/inner/findActivity")
    public Map findactivityRule(@RequestBody List<Long> skuidList){
        //map中封装了每件商品以及其对应的活动规则,SKUID为key,List<String> 活动规则列表为value
        Map<Long, List<String>> map=
                activityInfoService.findActivity(skuidList);
       /* Set<Long> longs = map.keySet();
        for (Long aLong : longs) {
            List<String> strings = map.get(aLong);
            for (String string : strings) {
                System.out.println("规则:"+string);
            }
        }*/
        return map;
    }

    //根据商品id和用户id,根据商品id查询商品参加的活动，根据用户id,查询有哪些优惠券
    @GetMapping("inner/findActivityAndCoupon/{skuId}/{userId}")
    public Map<String, Object> findActivityAndCoupon(@PathVariable Long skuId,
                                                     @PathVariable("userId") Long userId) {
        //返回的数据中包含活动列表和优惠券列表
        return activityInfoService.findActivityAndCoupon(skuId, userId);
    }

    //根据购物车商品列表，查看参与的活动和优惠券
    @PostMapping("inner/findCartActivityAndCoupon/{userId}")
    public OrderConfirmVo findCartActivityAndCoupon(@RequestBody List<CartInfo> cartInfoList,
                                                    @PathVariable("userId") Long userId){
        return activityInfoService.findCartActivityAndCoupon(cartInfoList,userId);
    }

    //根据传递过来的购物车商品信息List<CartInfo> 查询这些商品参加的活动信息，返回类型: List<CartInfoVo>
    //获取购物车的活动信息
    @PostMapping("inner/findCartActivityList")
    public List<CartInfoVo> findCartActivityList(@RequestBody  List<CartInfo> cartInfoList){
        return activityInfoService.findCartActivityList(cartInfoList);
    }

    @Autowired
    private CouponInfoService couponInfoService;
    //获取购物车中优惠券的信息
    @PostMapping("inner/findRangeSkuIdList/{couponId}")
    public CouponInfo findRangeSkuIdList(@RequestBody List<CartInfo> cartInfoList,
                                         @PathVariable("couponId")Long couponId){
        return couponInfoService.findRangeSkuIdList(cartInfoList,couponId);
    }

    @Autowired
    private CouponUseService couponUseService;
    //更新优惠券状态
    @GetMapping(value = "inner/updateCouponInfoUseStatus/{couponId}/{userId}/{orderId}")
    public  void updateCouponInfoUseStatus(
            @PathVariable("userId") Long userId,
            @PathVariable("couponId") Long couponId,
            @PathVariable("orderId") Long orderId){
        couponUseService.updateCouponInfoUseStatus(userId,couponId,orderId);
    }

}
